package com.yzy.soft.system.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.google.common.collect.Lists;
import com.yzy.soft.common.persistence.dao.HtmlCarouselMapper;
import com.yzy.soft.common.persistence.model.HtmlButton;
import com.yzy.soft.common.persistence.model.HtmlCarousel;
import com.yzy.soft.common.persistence.model.HtmlProgressBar;
import com.yzy.soft.core.base.controller.BaseController;
import com.yzy.soft.core.base.tips.ErrorTip;
import com.yzy.soft.core.base.tips.Tip;
import com.yzy.soft.system.dto.TableDataDto;
import com.yzy.soft.system.freemarker.SynchronousMetadata;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.RowBounds;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
* 描述：测试控制层
* @author Yinzhiyuan
* @date 2018-12-18 14:54:01
*/
@Api(value="测试控制层")
@RestController
@RequestMapping("/htmlcarousel")
public class HtmlCarouselController extends BaseController{


    private static final Logger logger=LoggerFactory.getLogger(HtmlCarouselController.class);
    //@Autowired
    //private IMetadataColumnControllerService metadataColumnControllerService;

    @Autowired
    private HtmlCarouselMapper htmlCarouselMapper;
    @Autowired
    private SynchronousMetadata synchronousMetadata;


    /**
    * 描述：查询 测试列表
    */
    @ApiOperation(value="查询测试")
    @GetMapping(value = "/page/{id}",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public TableDataDto getAll(@RequestParam(name = "page", defaultValue = "1", required = true) int page, @RequestParam(name = "limit", defaultValue = "10", required = true) int pageSize)throws Exception {

        Wrapper<HtmlCarousel> metadataColumnWrapper=new EntityWrapper<>();
        page=(page-1)*pageSize;
        pageSize=page+pageSize;
        List<HtmlCarousel> result=htmlCarouselMapper.selectPage(new RowBounds(page,pageSize),metadataColumnWrapper);
        TableDataDto tableDataDto=new TableDataDto();
        tableDataDto.setCode("00");
        tableDataDto.setMsg("000");
        tableDataDto.setCount(htmlCarouselMapper.selectCount(metadataColumnWrapper));
        tableDataDto.setData(result);
        return tableDataDto;
    }

    /**
    * 描述：根据Id 查询 测试
    * @param id  测试id
    */
    @ApiOperation(value="查询测试")
    @GetMapping(value = "/{id}",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public HtmlCarousel findById(@PathVariable("id") Integer id)throws Exception {
        return htmlCarouselMapper.selectById(id);
    }

    /**
    * 描述:创建测试
    * @param metadataColumnControllerDTO  测试DTO
    */
    @ApiOperation(value="创建测试")
    @PostMapping("")
    public Tip create(@ModelAttribute HtmlCarousel htmlButton) {
        htmlButton.setCreatetime(new Date());
        htmlButton.setLastupdatetime(new Date());
        Integer result=htmlCarouselMapper.insert(htmlButton);
        return SUCCESS_TIP;
    }

   /**
     * 描述：删除测试
     * @param id 测试id
     */
    @ApiOperation(value="删除测试")
    @DeleteMapping("/{id}")
    public Tip delete(@RequestParam(name="ids[]",required = false) String[] htmlTables,@PathVariable(name="id",required = false) String id) {
        //Collections.<String>
        if(StringUtils.isNotBlank(id)){
            Integer result=htmlCarouselMapper.deleteById(id);
        }else if(htmlTables.length!=0){
            Integer result=htmlCarouselMapper.deleteBatchIds(Lists.newArrayList(htmlTables) );
        }
        return SUCCESS_TIP;
    }
    @ApiOperation(value="批量删除测试")
    @DeleteMapping("/")
    public Tip delete1(@RequestParam(name="ids[]",required = false) String[] metadataColumnController) {
        if(metadataColumnController.length!=0){
           Integer result=htmlCarouselMapper.deleteBatchIds(Lists.newArrayList(metadataColumnController) );
        }
        return SUCCESS_TIP;
    }

    @ApiOperation(value="批量删除测试")
    @DeleteMapping("/carouselid/{carouselId}")
    public Tip deleteCarouselid_(@RequestParam(value="carouselids[]",required = false) String[] progressbarids,@PathVariable(name="carouselId",required = false) String progressbarId) {
        logger.info("HtmlCarouseController deleteCarouselid_ {}", JSONObject.toJSONString(progressbarids));
        if(null!=progressbarids&&progressbarids.length!=0){
            logger.info("HtmlCarouseController deleteCarouselid_ 大小为： {}", Lists.newArrayList(progressbarids).size());
            Integer result=htmlCarouselMapper.deleteBatchCarouselIds_(Lists.newArrayList(progressbarids) );
            return SUCCESS_TIP;
        }
        Integer result=htmlCarouselMapper.deleteCarouselid_(progressbarId);

        return SUCCESS_TIP;
    }

    /**
    * 描述：更新测试
    * @param id 测试id
    */
    @ApiOperation(value="更新测试")
    @PutMapping(value = "/{id}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Tip updateMetadataColumnController(@ModelAttribute HtmlCarousel htmlButton) throws Exception {
        htmlButton.setLastupdatetime(new Date());
        Integer result=htmlCarouselMapper.updateById(htmlButton);
        return SUCCESS_TIP;
    }

    @GetMapping(value ="/by",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public TableDataDto getCadtion(@ModelAttribute HtmlButton htmlButton,@RequestParam(name = "page", defaultValue = "1", required = true) int page, @RequestParam(name = "limit", defaultValue = "10", required = true) int pageSize) {

            Wrapper<HtmlProgressBar> metadataColumnWrapper=new EntityWrapper<>();
            page=(page-1)*pageSize;
            pageSize=page+pageSize;
        Wrapper<HtmlCarousel> wrapper = new EntityWrapper<>();
//        wrapper.where("table_id={0}", htmlTable.getTableId());
        List<HtmlCarousel> result = htmlCarouselMapper.selectPage(new RowBounds(page,pageSize),wrapper);
        TableDataDto tableDataDto = new TableDataDto();
        tableDataDto.setCode("00");
        tableDataDto.setMsg("000");
        tableDataDto.setCount(result.size());
        tableDataDto.setData(result);
        return tableDataDto;
    }

    @PutMapping("/status/{id}")
    public Tip status(@PathVariable("id")Integer id) {
        Integer result=htmlCarouselMapper.status(id);
        if(null==result){
            return new ErrorTip(500,"s");
        }
        return SUCCESS_TIP;
    }

    @GetMapping("/metadataColumnControllerId/{metadataColumnControllerId}")
    public TableDataDto getbyAccountId(@PathVariable(name="metadataColumnControllerId") String metadataColumnControllerId,@RequestParam(name="page",defaultValue = "1",required = true) int page, @RequestParam(name="limit",defaultValue = "10",required = true) int pageSize) {
       Wrapper<HtmlCarousel> htmlTableWrapper=new EntityWrapper<>();
        htmlTableWrapper.where("accountid={0}",metadataColumnControllerId);
       page=(page-1)*pageSize;
       pageSize=page+pageSize;
       List<HtmlCarousel> result=htmlCarouselMapper.selectPage(new RowBounds(page,pageSize),htmlTableWrapper);
       TableDataDto tableDataDto=new TableDataDto();
        tableDataDto.setCode("00");
        tableDataDto.setMsg("000");
        tableDataDto.setCount(htmlCarouselMapper.selectCount(htmlTableWrapper));
        tableDataDto.setData(result);
        return tableDataDto;
    }
    @ApiOperation("批量将费用归档设置成弃用状态")
    @PostMapping("/batchNuUse")
    public Tip batchNuUse(@RequestParam(name="ids[]",required = false) Integer[] metadataColumnController) {
        if(metadataColumnController.length!=0){
           Integer result=htmlCarouselMapper.batchNuUseIds(Lists.newArrayList(metadataColumnController) );
        }
        return SUCCESS_TIP;
    }
    @ApiOperation("批量将费用归档设置成可用状态")
    @PostMapping("/batchUse")
    public Tip batchUse(@RequestParam(name="ids[]",required = false) Integer[] metadataColumnController) {
        if(metadataColumnController.length!=0){
            Integer result=htmlCarouselMapper.batchUseIds(Lists.newArrayList(metadataColumnController) );
        }
        return SUCCESS_TIP;
    }
    @GetMapping("/synchronous/")
    public Tip synchronous(@RequestParam(name="ids[]",required = false) Integer[] metadataTableIds,@RequestParam(name="id",required = false) Integer metadataTableId){
        if(metadataTableIds.length!=0){
            try {
                synchronousMetadata.synchronousMetadataColum(Lists.newArrayList(metadataTableIds));
            } catch (Exception e) {
                e.printStackTrace();
                return new ErrorTip(500,e.getMessage());
            }


        }
//        SynchronousMetadata synchronousMetadata=new SynchronousMetadata();
        try {
            synchronousMetadata.synchronousMetadataColum(metadataTableId);
        } catch (Exception e) {
            e.printStackTrace();
            return new ErrorTip(500,e.getMessage());
        }
//        if(null==result){
//            return new ErrorTip(500,"s");
//        }
        return SUCCESS_TIP;
    }

}